<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <title>添加点标记</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
        body,
        html,
        #container {
            overflow: hidden;
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>
    <script src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=gktwARGFRZcfumx9y9vml1vntQI6cCoO"></script>
</head>

<body>
    <div id="container"></div>
</body>

</html>
<script>
    window.onload = function () {

        navigator.geolocation.getCurrentPosition((res) => {
            var data = res.coords;
            console.log(data.longitude);//113.84259377617724 
            console.log(data.latitude);//22.603501629611138
            //创建地图的对象,
            var map = new BMapGL.Map('container');//113.854457,22.60646
            var point = new BMapGL.Point(data.longitude, data.latitude);
            //设置中心点,地图的放大缩小比例
            map.centerAndZoom(point, 20);
            // 可以滚轮手动缩放
            map.enableScrollWheelZoom(true);
            // 创建点标记
            var marker1 = new BMapGL.Marker(point);

            // 在地图上添加点标记
            map.addOverlay(marker1);

            //获取 该地理位置在哪个城市
            var gc = new BMapGL.Geocoder();
            gc.getLocation(point, function (rs) {
                console.log(rs.addressComponents.city);
            })

        }, (err) => {
            console.log(err);
        }, {
            enableHighAccuracy: true
        })


    }




</script>